package com.gwcm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gwcm.exception.BusinessException;
import com.gwcm.model.dto.category.CategoryQueryRequest;
import com.gwcm.model.dto.orderInfo.PosSaleOrderQueryRequest;
import com.gwcm.model.entity.Product;
import com.gwcm.model.vo.CategoryVo;
import com.gwcm.model.vo.PosSaleOrderVo;
import com.gwcm.model.vo.SaleItemVo;
import com.gwcm.model.vo.SaleStatisticalByMonthVo;
import com.gwcm.service.ISaleService;
import com.gwcm.service.ProductService;
import com.yhz.commonutil.common.BaseResponse;
import com.yhz.commonutil.common.ErrorCode;
import com.yhz.commonutil.common.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.codehaus.jackson.map.MapperConfig;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;

@RestController
@RequestMapping("sale")
@Api(tags = "销售接口")
public class SaleController {
    @Resource
    private ISaleService saleService;
    @Resource
    private ProductService productService;

    /**
     * 开始一次新的销售
     * @return
     */
    @GetMapping("/makeNewSale")
    @ApiOperation("开始一次新的销售")
    public BaseResponse makeNewSale(){
        return ResultUtils.success(saleService.makeNewSale());
    }

    /**
     * 输入商品
     * @param itemSn 产品编号
     * @param quantity 数量
     * @return
     * 代码有部分业务逻辑，不符合controller内聚设计要求。
     * 这样写的原因是因为代码来自OOAD案例的Register，
     * Register是门面控制器，本身是domain类，允许写少量业务逻辑。
     */
    @GetMapping("/enterItem")
    @ApiOperation("输入商品")
    public BaseResponse enterItem(@RequestParam("itemSn") String itemSn, @RequestParam("quantity") int quantity){
        SaleItemVo saleItemVo = null;
        Product product = productService.getProductByProductSn(itemSn);
        if(product != null){
            saleService.makeLineItem(product, quantity);
            saleItemVo = new SaleItemVo(itemSn, product.getName(), product.getPrice(), quantity);
            return ResultUtils.success(saleItemVo);
        }
        return ResultUtils.error(ErrorCode.PARAMS_ERROR);
    }

    /**
     * 结束销售
     * @return
     */
    @GetMapping("/endSale")
    @ApiOperation("结束销售")
    public BaseResponse endSale(){
        return ResultUtils.success(saleService.endSale());
    }

    /**
     * 确认支付
     * @param cashTendered 支付金额
     * @return
     */
    @GetMapping("/makePayment")
    @ApiOperation("确认支付")
    public BaseResponse makePayment(@RequestParam("cashTendered")BigDecimal cashTendered){
        return ResultUtils.success(saleService.makePayment(cashTendered));
    }

    @PostMapping("/changeQuantity")
    @ApiOperation("修改一行订单明细数量")
    public BaseResponse changeQuantity(@RequestBody SaleItemVo saleItemVo){
        saleService.changeQuantityOfSaleItem(saleItemVo.getItemSn(),saleItemVo.getQuantity());
        return ResultUtils.success("修改订单明细数量成功");
    }
    @GetMapping("/deleteSaleItem/{itemSn}")
    @ApiOperation("删除一行订单明细")
    public BaseResponse deleteSaleItem(@PathVariable("itemSn") String itemSn){
        saleService.deleteSaleItem(itemSn);
        return ResultUtils.success("删除订单明细成功");
    }

   @ApiOperation("按月份统计销售量、销售额")
   @GetMapping("/statisticalByMonth")
    public BaseResponse statisticalByMonth(String year) {
       SaleStatisticalByMonthVo saleStatisticalByMonthVo=saleService.statisticalByMonth(year);
       return ResultUtils.success(saleStatisticalByMonthVo);
    }
}
